﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sample
{
    public partial class {TableName}Form : System.Web.UI.Page
    {
        public enum DisplayType
        {
            Edit, Add
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if(GetDisplayType == DisplayType.Add)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                    {TableColumns}{ExcludeColumn:Id}{ForeignKeyParent:6,}
                    {/TableColumns}}
                }            
                if (GetDisplayType == DisplayType.Edit)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                        {TableName} entity = db.{TableName}s.Single(_ => _.Id == GetEditID);
                        {TableColumns}{ExcludeColumn:Id}{ForeignKeyParent:3,1,0};
                        {/TableColumns}}
                }
            }
        }
        {TableColumns}{ForeignKeyParent:5,}
        {/TableColumns}public DisplayType GetDisplayType
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                    return DisplayType.Edit;
                else
                    return DisplayType.Add;
            }
        }
        private int GetEditID
        {
            get { return int.Parse(Request.QueryString["ID"]); }
        }
        protected void {TableName}SaveButton_Click(object sender, EventArgs e)
        {
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                {TableName} entity;
                if (GetDisplayType == DisplayType.Edit)
                    entity = db.{TableName}s.Single(_ => _.Id == GetEditID);
                else
                {
                    entity = new {TableName}();
                    db.{TableName}s.InsertOnSubmit(entity);
                }
                {TableColumns}{ExcludeColumn:Id}{ForeignKeyParent:4,2};
                {/TableColumns}db.SubmitChanges();
            }
        }
        protected void {TableName}CancelButton_Click(object sender, EventArgs e)
        {

        }
    }
}